#include <leetcode/leecode.h>
#include<vector>
#include <iostream>
#include <array>
#include <stdlib.h>

#include <cstdint>


class Solution {
public:
  int threeSumClosest(vector<int>& nums, int target) {
    int result = 0;
    int abs = INT32_MAX;
    int vsize = nums.size();
    for (int i = 0; i < vsize - 2; i++) {
      for (int j = i + 1; j < vsize - 1;j++) {
        for (int k = j + 1; k < vsize; k++) {
          int abs_temp = nums[i] + nums[j] + nums[k] - target;
          if (abs_temp < 0) abs_temp = 0 - abs_temp;
          if (abs_temp < abs) {
            abs = abs_temp;
            result = nums[i] + nums[j] + nums[k];
          }
         // if(  )
        }
      }
    
    }

    return result;
  }
};

void TestThreeSumClosest(vector<int>& nums, int target) {
  Solution s;
  auto ret = s.threeSumClosest(nums, target);

  cout << "input is: " << endl;
  cout << target << endl;
  for (int i = 0; i < nums.size(); i++)
    cout << nums[i] << ",";
  cout << endl;

  cout << "result is: " << ret << endl;

}

TEST(TestThreeSumClosest, t1) {
  GTEST_SKIP();
  std::vector<int> vec1{ -1,2,1,-4 };
  int target1 = 1;
  TestThreeSumClosest(vec1, 1);

  std::vector<int> vec2{ 0, 0, 0 };
  int target2 = 1;
  TestThreeSumClosest(vec2, target2);
}

TEST(TestThreeSumClosest, t2) {
  GTEST_SKIP();
  std::vector<int> vec2{ -31,684,-416,948,231,-602,696,-12,219,176,207,-455,-50,827,659,292,-182,-879,-939,881,919,-908,346,825,-957,-793,527,-156,-582,148,-176,398,-22,-508,-5,2,517,133,939,-526,-621,-129,963,321,-105,-559,-405,871,465,-248,-123,979,758,432,309,498,387,627,538,-357,701,-110,717,-846,-112,58,210,-799,-520,-681,-852,-732,543,829,-492,-755,-820,-994,775,575,-752,-631,-771,-400,107,45,-545,10,8,497,901,-725,-164,850,713,832,227,-487,79,-798,-614,600,-974,930,56,643,709,221,-854,-633,-578,518,501,-71,-650,-429,-891,261,168,-404,-421,-863,268,-457,-87,251,888,115,-132,-512,903,-484,624,22,-440,-629,404,-594,-306,-388,67,-515,394,814,-219,687,236,539,330,-848,-307,-55,-469,-535,-734,357,-56,-649,-585,-75,-403,-32,706,-65,-268,62,831,-956,-27,-69,-651,-72,642,-680,617,-903,-475,-89,-197,279,-444,712,244,54,874,-291,-805,-824,-676,114,933,283,197,733,594,-710,-143,522,724,-423,688,914,-729,-661,651,345,-181,658,-684,-281,-698,910,-30,277,-99,605,836,-894,-109,-781,-947,-49,602,848,-784,469,156,-900,-632,572,204,-294,11,-200,-385,817,294,-210,141,95,-965,-369,794,637,-735,-347,510,762,-377,513,-504,-165,-477,-178,32,55,-948,-595,-64,-170,-576,776,-901,-518,-537,-215,-351,-458,834,-15,-376,-381,-160,142,408,-275,-987,533,-498,-1000,72,-810,-689,639,-816,791,920,-344,215,-934,-880,-935,655,-577,-616,-823,212,714,-499,489,567,647,324,468,-797,-61,646,-572,-392,482,-240,450,154,681,-301,-839,-607,-895,-245,-704,-929,60,895,116,-638,-90,562,75,665,945,768,678,137,-636,436,-282,17,657,882,740,-94,-536,-748,-870,-261,461,335,223,-137,139,420,356,102,-809,-172,803,-39,-862,399,666,540,-187,-151,579,694,82,386,570,-583,-113,636,-973,635,830,996,-843,746,-482,929,840,-471,363,14,936,34,-926,-25,276,103,206,-163,679,-731,-470,789,203,-700,-340,-643,490,-476,245,-510,-452,160,-806,629,-221,118,-365,-243,287,926,-467,-428,977,-909,788,-804,634,-652,-570,609,26,-587,-322,-737,935,-717,456,-615,991,135,-410,-157,660,778,641,971,-822,-104,-493,-238,350,-726,-768,-540,-773,670,550,406,-889,-703,879,-765,-318,847,7,-141,-610,950,-750,44,119,189,341,-292,-620,462,438,153,921,-82,723,-514,-288,-252,-530,-641,339,-795,-675,-702,-865,246,94,-549,205,-154,-529,620,-468,21,854,-485,-233,-657,-300,-173,-162 };
  int target2 = -4546;
  TestThreeSumClosest(vec2, target2);

}
